<?php
	error_reporting(E_ALL || ~E_NOTICE);

	$yehangshu = 10;

	if($_POST["rows"])
	{
		$yehangshu = (int)$_POST["rows"];
	}
//	file_put_contents("test.log", "yehangshu: ".$yehangshu." line ".__LINE__."\n", FILE_APPEND);

	$controlerpagenum = 1;
	$pagestart=1;
	$pageend=1;
//	$i=1;
	$j=1;   
	$s='';
//	$sqlstr='';
//		$blank=false;
	$accesstab="";
	$dbResult=array();
	$rowsnum=array();
//		if($_POST['page'])
	if($_POST["page"])
	{
		$controlerpagenum = (int)$_POST["page"];
//			$controlerpagenum = $_POST['page'];
	}
	else
	{
		$controlerpagenum = 1;
	}
//		file_put_contents("test.log", "controlerpagenum: ".$controlerpagenum, FILE_APPEND);
	$myconn=dwopendb();
	$strSql="select COUNT(*) from staffbasesetting;";
	$result=mysql_query($strSql,$myconn);	        
	$rowsnum = mysql_fetch_array($result);
//	        @file_put_contents("test.log", "rowsnum[0]:".$rowsnum[0]."\n", FILE_APPEND);

	mysql_free_result($result);
	$pagemax=1;
	$pagemax = (($rowsnum[0]/($yehangshu*1.0))-(int)($rowsnum[0]/($yehangshu*1.0)))>0.?(int)($rowsnum[0]/($yehangshu*1.0))+1:(int)($rowsnum[0]/($yehangshu*1.0));
	if($pagemax<1) $pagemax=1;

//	    
	if($controlerpagenum>$pagemax) $controlerpagenum=$pagemax;
	$pagestart=$yehangshu*$controlerpagenum-$yehangshu;
	$pageend=$yehangshu;

//	file_put_contents("test.log","pagemax: ".$pagemax." yehangshu: ".$yehangshu." controlerpagenum: ".$controlerpagenum." rowsnum[0]: ".$rowsnum[0]." line ".__LINE__."\n", FILE_APPEND);
	$rowid='';
	$rowid=$_POST["rowid"];

	$lie = array('rowid','staffnum','staffname','cardnum','departmentname','departmentID','controlerNum','gatenum','periodofvalitystart','periodofvalityend','staffgender','staffage','picfilename'); 

	$strSql="select ";

	$zonglieshu = count($lie);
	
//	file_put_contents("test.log", "zonglieshu =".$zonglieshu." lie ".$lie." line ".__LINE__."\n", FILE_APPEND);
	
	for($i=0;$i<$zonglieshu;$i++)
	{
		if($i != ($zonglieshu-1))
		{
			$strSql .= $lie[$i].",";
		} else
		{
			$strSql .= $lie[$i];
		}
	}
//	file_put_contents("test.log", "strSql =".$strSql." line ".__LINE__."\n", FILE_APPEND);
//	
	$strSql .= " from staffbasesetting";

	if($rowid)
	{
		$strSql .= " where rowid like '%".$rowid."%'";
	}

	$strSql .= " ORDER BY rowid DESC limit ".$pagestart.','.$pageend.';';
	 
//    file_put_contents("test.log", "strSql =".$strSql." line ".__LINE__."\n", FILE_APPEND);
	
	$result=mysql_query($strSql,$myconn);		
	
	while($dbResult = mysql_fetch_array($result))
	{	        
		for($j=1;$j<=$zonglieshu;$j++)
		{
			if($j==1)
			{
				$accesstab .= "{\"".$lie[$j-1]."\":\"".$dbResult[($j-1)].'",';
			}else if($j==$zonglieshu)
			{
			 	$accesstab .= "\"".$lie[$j-1]."\":\"".$dbResult[($j-1)].'"},';
			}
			else
			{
				if($dbResult[($j-1)]==="NULL")
				{
					$accesstab .= "\"".$lie[$j-1]."\":\"空\",";
				}else if($dbResult[($j-1)]==="wu")
				{
					$accesstab .= "\"".$lie[$j-1]."\":\"无\",";
				}else if($dbResult[($j-1)]==="ALL" ||$dbResult[($j-1)]==="all")
				{
					$accesstab .= "\"".$lie[$j-1]."\":\"全部\",";
				}else
				{
					$accesstab .= "\"".$lie[$j-1]."\":\"".$dbResult[($j-1)].'",';
				}
			} 				
		}		
	}
	mysql_free_result($result);
	dwclosedb($myconn);

	$i=(int)$rowsnum[0];
	$chuanchangdu = strlen($accesstab);
	if($chuanchangdu==1)
	{
		$accesstab = "{\"rows\":[],\"total\":0}";
	}else if($chuanchangdu>1)
	{
		$accesstab[$chuanchangdu-1] = ']'; 
		$accesstab = "{\"rows\":[".$accesstab.",\"total\":$i}";
	}		
//	file_put_contents("test.log", "accesstab :".$accesstab." line ".__LINE__."\n", FILE_APPEND);
		
	echo $accesstab;

	function dwclosedb($myconn)
	{
		mysql_close($myconn);	
	}
	
	function dwopendb()
	{
		$myconn=@mysql_connect("localhost","dw","DWdawu-309");
		if(!$myconn)
		{
//			die('Could not connect: '.mysql_error());
			$string = "Can't connect database : ".mysql_error()." LINE:".__LINE__."\n";
			file_put_contents("test.log", $string, FILE_APPEND);
			return false;
		}
		$db_selected=mysql_select_db("dwzuapairenlian",$myconn);
		if(!$db_selected)
		{
//			die("Can't use database dwaccessDB : ".mysql_error());       
			$strSql="CREATE DATABASE dwzuapairenlian;";
			$result=mysql_query($strSql,$myconn);
			if($result==false)
			{
				die("Can't use database dwaccessDB : ".mysql_error());
				$string = "Can't use database : ".mysql_error()." LINE:".__LINE__."\n";
				file_put_contents("test.log", $string, FILE_APPEND);
				dwclosedb($myconn);
				$response = base64_encode("{\"res\":\"deny3\"}");
				echo $response;					
				return false;
			}
			$strSql="set character_set_database=utf8;";
			$result=mysql_query($strSql,$myconn);
	
			mysql_close($myconn);
			return false;
		}
		return $myconn;
	}
?>
